package gui;

import java.awt.Color;
import java.awt.Component;
import java.awt.event.MouseEvent;
import java.util.Arrays;
import java.util.List;

import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

/**
 * This class creates a default JTable used in our UI's.
 * The settings of the table are set here so everyone can enjoy it with minimum work.
 */

public class SimpleTable
{
	public DefaultTableModel model;
	public JTable table;
	public JScrollPane tblScrollPane;
	
	List<Color> rowColours = Arrays.asList(
	        Color.RED,
	        Color.GREEN,
	        Color.CYAN
	    );
	
	/**
	 * Creates a new JTable at the specified settings.
	 * @param rowData The data in the cells
	 * @param colData The data in the column header.
	 * @param x	X bounds
	 * @param y	Y bounds
	 * @param w	Width
	 * @param h	Height
	 */
	public SimpleTable(Object[][] rowData, Object[] colData,int x,int y,int w, int h)
	{
		model = new DefaultTableModel(rowData,colData);
		table = new JTable(model) 
		{
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			public boolean isCellEditable(int rowIndex, int colIndex) 
			{
				return false; //Disallow the editing of any cell
	    	}
			public String getToolTipText(MouseEvent event)
			{
				int selected_col  = convertColumnIndexToModel(columnAtPoint(event.getPoint()));
				int selected_row  = convertRowIndexToModel(rowAtPoint(event.getPoint()));
				return getValueAt(selected_row, selected_col).toString();
			}
		};
		tblScrollPane = new JScrollPane(table);
		init(x,y,w,h);
	}
	
	/**
	 * Initializes the table.
	 * @param x	X bounds
	 * @param y	Y bounds
	 * @param w	Width
	 * @param h	Height
	 */
	private void init(int x,int y,int w, int h)
	{
		table.getTableHeader().setResizingAllowed(false);
		table.getTableHeader().setReorderingAllowed(false);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		tblScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		tblScrollPane.setBounds(x,y,w,h);
		table.setOpaque(false);
		tblScrollPane.setOpaque(false);
		tblScrollPane.getViewport().setOpaque(false);
		//((DefaultTableCellRenderer)table.getDefaultRenderer(Object.class)).setOpaque(false);
	}
	
	/**
	 * Sets a specific column width
	 * @param col The column to set
	 * @param w	The width
	 */
	public void setColWidth(int col,int w)
	{
		TableColumn column = table.getColumnModel().getColumn(col);
        column.setPreferredWidth(w);
	}
	
	/**
	 * Sets a number of columns to be centered
	 * @param numberOfColumns The number of columns to be centered
	 */	
	public void setColCenter(int numberOfColumns)
	{
		for(int i = 0 ; i < numberOfColumns ; i++)
		{ 
			TableColumn col = this.table.getColumnModel().getColumn(i);  
	        DefaultTableCellRenderer dtcr = new DefaultTableCellRenderer();    
	        dtcr.setHorizontalAlignment(SwingConstants.CENTER);  
	        col.setCellRenderer(dtcr);  
		}
	}	
	
	/**
	 * Sets row background color
	 * @param row	the row to set color to.
	 * @param c	is the color to set.
	 */		
    public void setRowColour(int row, Color c) 
    {
        rowColours.set(row, c);
        model.fireTableRowsUpdated(row, row);
    }

	/**
	 * Gets row background color
	 * @param row	the row to get color from.
	 * @return the color of the row.
	 */    
    public Color getRowColour(int row) 
    {
        return rowColours.get(row);
    }
	
	/**
	 * This class is used to set the row background colors. 
	 */   
	static class MyTableCellRenderer extends DefaultTableCellRenderer 
	{
		private static final long serialVersionUID = 1L;
		SimpleTable st;
			  
		public MyTableCellRenderer(SimpleTable st) 
		{
			this.st=st;
		}
		      
		@Override
		public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) 
		{
			Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
			c.setBackground(st.getRowColour(row));
		    return c;
		 }
	}
}

class SelectionListener implements ListSelectionListener 
{
	  JTable table;

	  SelectionListener(JTable table) 
	  {
	    this.table = table;
	  }
	  public void valueChanged(ListSelectionEvent e) 
	  {
		  /*
	    if (e.getValueIsAdjusting()) 
	    {
	      System.out.println("The mouse button has not yet been released");
	    }*/
	    System.out.println("CELL= "+table.getSelectedColumn() + ":" + table.getSelectedRow());
	  }
}

